<?php

/**
 * Pozo form base class.
 *
 * @method Pozo getObject() Returns the current form's model object
 *
 * @package    prode
 * @subpackage form
 * @author     Your name here
 */
abstract class BasePozoForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                    => new sfWidgetFormInputHidden(),
      'anho'                  => new sfWidgetFormInputText(),
      'torneo_id'             => new sfWidgetFormPropelChoice(array('model' => 'Torneo', 'add_empty' => false)),
      'temporada_id'          => new sfWidgetFormPropelChoice(array('model' => 'Temporada', 'add_empty' => false)),
      'fechanro'              => new sfWidgetFormInputText(),
      'habilitado'            => new sfWidgetFormInputCheckbox(),
      'montoinicial'          => new sfWidgetFormInputText(),
      'totalpagado'           => new sfWidgetFormInputText(),
      'montofinal'            => new sfWidgetFormInputText(),
      'created_at'            => new sfWidgetFormDateTime(),
      'detalleganadores_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'sfGuardUser')),
      'detallepozo_list'      => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Apuesta')),
    ));

    $this->setValidators(array(
      'id'                    => new sfValidatorPropelChoice(array('model' => 'Pozo', 'column' => 'id', 'required' => false)),
      'anho'                  => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647)),
      'torneo_id'             => new sfValidatorPropelChoice(array('model' => 'Torneo', 'column' => 'id')),
      'temporada_id'          => new sfValidatorPropelChoice(array('model' => 'Temporada', 'column' => 'id')),
      'fechanro'              => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647)),
      'habilitado'            => new sfValidatorBoolean(array('required' => false)),
      'montoinicial'          => new sfValidatorNumber(array('required' => false)),
      'totalpagado'           => new sfValidatorNumber(array('required' => false)),
      'montofinal'            => new sfValidatorNumber(array('required' => false)),
      'created_at'            => new sfValidatorDateTime(array('required' => false)),
      'detalleganadores_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'required' => false)),
      'detallepozo_list'      => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Apuesta', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('pozo[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Pozo';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['detalleganadores_list']))
    {
      $values = array();
      foreach ($this->object->getDetalleganadoress() as $obj)
      {
        $values[] = $obj->getSfGuardUser();
      }

      $this->setDefault('detalleganadores_list', $values);
    }

    if (isset($this->widgetSchema['detallepozo_list']))
    {
      $values = array();
      foreach ($this->object->getDetallepozos() as $obj)
      {
        $values[] = $obj->getApuestaId();
      }

      $this->setDefault('detallepozo_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveDetalleganadoresList($con);
    $this->saveDetallepozoList($con);
  }

  public function saveDetalleganadoresList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['detalleganadores_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(DetalleganadoresPeer::POZO_ID, $this->object->getPrimaryKey());
    DetalleganadoresPeer::doDelete($c, $con);

    $values = $this->getValue('detalleganadores_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Detalleganadores();
        $obj->setPozoId($this->object->getPrimaryKey());
        $obj->setSfGuardUser($value);
        $obj->save();
      }
    }
  }

  public function saveDetallepozoList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['detallepozo_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(DetallepozoPeer::POZO_ID, $this->object->getPrimaryKey());
    DetallepozoPeer::doDelete($c, $con);

    $values = $this->getValue('detallepozo_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Detallepozo();
        $obj->setPozoId($this->object->getPrimaryKey());
        $obj->setApuestaId($value);
        $obj->save();
      }
    }
  }

}
